import utils.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Author ZhangCuirong
 * @Date 2025/9/3 11:06
 * @description:
 */
public class Solution104 {

    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftMaxDepth = root.left != null ? maxDepth(root.left) : 0;
        int rightMaxDepth = root.right != null ? maxDepth(root.right) : 0;
        return Math.max(leftMaxDepth, rightMaxDepth) + 1;
    }
    public int maxDepth2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int depth = 0;
        while (!queue.isEmpty()){
            int size = queue.size();
            depth++;
            for (int i = 0; i < size; i++) {
                TreeNode temp = queue.poll();
                if (temp != null && temp.left != null) {
                    queue.add(temp.left);
                }
                if (temp != null && temp.right != null) {
                    queue.add(temp.right);
                }
            }
        }
        return depth;
    }

    public static void main(String[] args) {
        Solution104 solution = new Solution104();
        TreeNode root = TreeNode.buildTree(new int[]{3, 9, 20, -1, -1, 15, 7}, -1);
        System.out.println(solution.maxDepth2(root));
        root = TreeNode.buildTree(new int[]{1, -1, 2}, -1);
        System.out.println(solution.maxDepth2(root));
    }
}
